<template>
  <div>
    <el-table
      ref="table"
      :data="tableData"
      :height="350"
      style="width: 100%"
    >
      <el-table-column label="#" width="50">
        <template slot-scope="scope">
          <el-radio
            v-model="selection._id"
            :label="scope.row._id"
            @change="selectAction(scope.row)"
          >&nbsp;
          </el-radio>
        </template>
      </el-table-column>
      <el-table-column label="选择用户" width="120">
        <template slot-scope="scope">{{ scope.row.name }}</template>
      </el-table-column>
      <el-table-column>
        <template #header="scope">
          <el-checkbox
            v-model="tag"
            :true-label="constant.INCLUDE_CHILD_DEPARTMENT"
            :false-label="constant.EXCLUDE_CHILD_DEPARTMENT"
            @change="fetchUsers()"
          >查询子部门用户
          </el-checkbox>
        </template>
        <template slot-scope="scope">{{ scope.row.orgName }}</template>
      </el-table-column>
      <el-table-column width="120">
        <template #header="scope">
          <div class="zlst-table-header--right">
            <a href="javascript:;" @click="showSelections">已选<em>{{ selection.userId === undefined ? 0 : 1 }}</em>人</a>
          </div>
        </template>
        <!-- <template slot-scope="scope">{{ scope.row | postName }}</template> -->
      </el-table-column>
    </el-table>
    <el-pagination
      v-if="tableTotal > 0"
      small
      background
      layout="total, prev, pager, next, jumper"
      :page-size="rows"
      :total="tableTotal"
      style="text-align: right; margin-top: 20px"
      @current-change="fetchUsers"
    />
  </div>
</template>

<script>
import { cloneDeep as clone } from 'lodash'
import EventBus from '../../bus'
import $http from '@/api/server/http'

// 是否包含子部门 0 为包含，1 为不包含
const INCLUDE_CHILD_DEPARTMENT = 0
const EXCLUDE_CHILD_DEPARTMENT = 1

export default {
  filters: {
    postName(row) {
      return row.posts.length ? row.posts[0].postName : ''
    }
  },
  model: {
    prop: 'value',
    event: 'change'
  },
  props: {
    value: {
      type: Object,
      default() {
        return {}
      }
    },
    orgId: {
      type: String,
      default: null
    },
    conditions: {
      type: Object,
      default() {
        return {}
      }
    }
  },
  data() {
    return {
      // 显示模式
      // api 模式 是从后台读取数据
      // selected 模式是列举出已选的数据
      mode: 'api',
      currentPage: -1,
      tableData: [],
      tableTotal: 0,
      tag: EXCLUDE_CHILD_DEPARTMENT,
      statusFlag: 1,
      rows: 20,
      selection: {},
      constant: {
        INCLUDE_CHILD_DEPARTMENT,
        EXCLUDE_CHILD_DEPARTMENT
      }
    }
  },
  watch: {
    orgId() {
      this.mode = 'api'
      this.fetchUsers()
    }
  },
  created() {
    EventBus.$on('orgSearchUserModal.searchUser', this.fetchUsersFromApi)
  },
  beforeDestroy() {
    EventBus.$off('orgSearchUserModal.searchUser', this.fetchUsersFromApi)
  },
  methods: {
    fetchUsers(page = 1) {
      this.currentPage = page
      if (this.mode === 'api') {
        this.fetchUsersFromApi(page)
      } else {
        this.fetchUsersFromSelections(page)
      }
    },
    fetchUsersFromApi(page = 1) {
      $http.post('humaninfoPage', {
        pageIndex: 1,
        pageSize: 9999,
        filters: [
          {
            condition: this.orgId,
            field: 'orgId',
            op: 'IS'
          }
        ]
      })
        .then(res => {
          if (res.data.resultCode !== 0) {
            return false
          }
          this.tableData = res.data.data
        })
    },
    fetchUsersFromSelections(page = 1) {
      this.tableTotal = this.selection.userId === undefined ? 0 : 1
      this.tableData = this.tableTotal ? [this.selection] : []
    },
    showSelections() {
      this.mode = 'selected'
      this.fetchUsers(1)
    },
    selectAction(row) {
      this.selection = clone(row)
      this.$emit('change', this.selection)
    }
  }
}
</script>

<style lang="scss" scoped>
  .el-table, .el-table__expanded-cell {
    background-color: #f8f9fa;
  }
  /deep/ .el-table th, /deep/ .el-table tr {
    background-color: #f8f9fa;
    .zlst-table-header--right {
      display: block;
      text-align: right;
      a em {
        color: #3ea9f5;
        font-style: normal;
      }
    }
  }
</style>
